package com.edol.painting.widget;

import android.content.Context;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.edol.painting.R;
import com.edol.painting.painting.Brush;

public class BrushToolContainerView extends LinearLayout {

    private static final String TAG = "BrushToolContainerView";

    private ImageView mBrushView = null;
    private ImageView mEraserView = null;
    private ImageView mTextureView = null;
    private ImageView mCutView = null;
    private ImageView mColorPickView = null;

    private LayerDrawable mBrushDrawable = null;
    private LayerDrawable mEraserDrawable = null;
    private LayerDrawable mTextureDrawable = null;
    private LayerDrawable mCutDrawable = null;

    private boolean mBrushFocus = true;
    private boolean mEraserFocus = false;
    private boolean mTextureFocus = false;
    private boolean mCutFocus = false;
    private boolean mColorPickFocus = false;
    
    private int mBrushType = Brush.BRUSH_PEN;
    private int mEraserType = Brush.BRUSH_ERASER;

    public BrushToolContainerView(Context context) {
        super(context);
        Log.i(TAG, "BrushToolContainerView(x)");
    }

    public BrushToolContainerView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        Log.i(TAG, "BrushToolContainerView(x,x,x)");
    }

    public BrushToolContainerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.i(TAG, "BrushToolContainerView(x,x)");
    }

    @Override
    protected void onFinishInflate() {
        Log.i(TAG, "onFinishInflate()");
        super.onFinishInflate();
        mBrushView = (ImageView)findViewById(R.id.action_brush);
        mEraserView = (ImageView)findViewById(R.id.action_eraser);
        mTextureView = (ImageView)findViewById(R.id.action_texture);
        mCutView = (ImageView)findViewById(R.id.action_cut);
        mColorPickView = (ImageView)findViewById(R.id.action_color_picker);

        mBrushDrawable = (LayerDrawable)mBrushView.getDrawable();
        mEraserDrawable = (LayerDrawable)mEraserView.getDrawable();
        mTextureDrawable = (LayerDrawable)mTextureView.getDrawable();
        mCutDrawable = (LayerDrawable)mCutView.getDrawable();

        updateItemView(R.id.action_brush);
    }
    
    public ImageView getColorPickView() {
    	return mColorPickView;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.i(TAG, "onMeasure()");
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.i(TAG, "onLayout() changed="+changed+",("+l+","+t+","+r+"," + b+")");
        super.onLayout(changed, l, t, r, b);
    }

    public void setOnItemClickListener(OnClickListener listener) {
        if (listener == null) return;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child != null && child instanceof ImageView) {
                child.setOnClickListener(listener);
            }
        }
    }

    public boolean getItemFocus(int id) {
        int resID = id;
        switch(resID) {
            case R.id.action_brush:
                return mBrushFocus;
            case R.id.action_eraser:
                return mEraserFocus;
            case R.id.action_texture:
                return mTextureFocus;
            case R.id.action_cut:
                return mCutFocus;
            case R.id.action_color_picker:
                return mColorPickFocus;
        }
        return false;
    }
    
    public void setControlsEnabled(boolean b) {
    	mBrushView.setEnabled(b);
    	mEraserView.setEnabled(b);
    	mTextureView.setEnabled(b);
    	mCutView.setEnabled(b);
    	mColorPickView.setEnabled(b);
    }
    
    public void updateBrushIcon(int brushType) {
    	if (mBrushFocus) {
	        mBrushDrawable.getDrawable(2).setLevel(brushType - 1);
	        mBrushType = brushType;
	        changeBrushAndEraserToolBackColor(mBrushType);
    	}
    }

    public void updateEraserIcon(int eraserType) {
    	mEraserDrawable.getDrawable(2).setLevel(eraserType == Brush.BRUSH_ERASER ? 0 : 1);
    	mEraserType = eraserType;
    	if (mEraserFocus) {
	        changeBrushAndEraserToolBackColor(mEraserType);
    	}
    }
    
	private void changeBrushAndEraserToolBackColor(int brushType) {
		int beColorId = 0;
		switch (brushType) {
		case Brush.BRUSH_PENCIL:
			beColorId = R.color.pencil_background;
			break;
		case Brush.BRUSH_PEN:
			beColorId = R.color.pen_background;
			break;
		case Brush.BRUSH_WATERCOLOR:
			beColorId = R.color.watercolor_background;
			break;
		case Brush.BRUSH_CHALK:
			beColorId = R.color.chalk_background;
			break;
		case Brush.BRUSH_MARKER:
			beColorId = R.color.marker_background;
			break;
		case Brush.BRUSH_SOFT_CHARCOAL:
			beColorId = R.color.soft_charcoal_background;
			break;
		case Brush.BRUSH_SOFT_ERASRE:
			beColorId = R.color.soft_eraser_background;
			break;
		case Brush.BRUSH_ERASER:
			beColorId = R.color.hard_eraser_background;
			break;
		}
		if (brushType == Brush.BRUSH_SOFT_ERASRE || brushType == Brush.BRUSH_ERASER) {
			mEraserDrawable.getDrawable(0).setAlpha(255);
			mEraserDrawable.getDrawable(0).setColorFilter(getResources().getColor(beColorId), Mode.SRC_IN);
		} else {
			mBrushDrawable.getDrawable(0).setAlpha(255);
			mBrushDrawable.getDrawable(0).setColorFilter(getResources().getColor(beColorId), Mode.SRC_IN);
		}
	}

    private static final int RB_IDX = 1;
    public void updateItemView(int id) {
        Log.i(TAG, "updateItemView(),id="+id);
        changeToolBackAlpha();
        int resID = id;
        switch(resID) {
            case R.id.action_brush:
            	changeBrushAndEraserToolBackColor(mBrushType);
                mBrushDrawable.getDrawable(RB_IDX).setAlpha(0);
                mEraserDrawable.getDrawable(RB_IDX).setAlpha(255);
                mCutDrawable.getDrawable(RB_IDX).setAlpha(255);
                mTextureDrawable.getDrawable(RB_IDX).setAlpha(255);

                mBrushFocus = true;
                mEraserFocus = false;
                mTextureFocus = false;
                mCutFocus = false;
                mColorPickFocus = false;
                break;
            case R.id.action_eraser:
            	changeBrushAndEraserToolBackColor(mEraserType);
                mEraserDrawable.getDrawable(RB_IDX).setAlpha(0);
                mBrushDrawable.getDrawable(RB_IDX).setAlpha(255);
                mCutDrawable.getDrawable(RB_IDX).setAlpha(255);
                mTextureDrawable.getDrawable(RB_IDX).setAlpha(255);

                mEraserFocus = true;
                mBrushFocus = false;
                mTextureFocus = false;
                mCutFocus = false;
                mColorPickFocus = false;
                break;
            case R.id.action_texture:
            	mTextureDrawable.getDrawable(0).setAlpha(255);
            	mTextureDrawable.getDrawable(0).setColorFilter(getResources().getColor(R.color.texture_picker_background), Mode.SRC_IN);
                mTextureDrawable.getDrawable(RB_IDX).setAlpha(0);
                mBrushDrawable.getDrawable(RB_IDX).setAlpha(255);
                mEraserDrawable.getDrawable(RB_IDX).setAlpha(255);
                mCutDrawable.getDrawable(RB_IDX).setAlpha(255);

                mTextureFocus = true;
                mBrushFocus = false;
                mEraserFocus = false;
                mCutFocus = false;
                mColorPickFocus = false;
                break;
            case R.id.action_cut:
            	mCutDrawable.getDrawable(0).setAlpha(255);
            	mCutDrawable.getDrawable(0).setColorFilter(getResources().getColor(R.color.cut_background), Mode.SRC_IN);
                mCutDrawable.getDrawable(RB_IDX).setAlpha(0);
                mBrushDrawable.getDrawable(RB_IDX).setAlpha(255);
                mEraserDrawable.getDrawable(RB_IDX).setAlpha(255);
                mTextureDrawable.getDrawable(RB_IDX).setAlpha(255);

                mCutFocus = true;
                mTextureFocus = false;
                mBrushFocus = false;
                mEraserFocus = false;
                mColorPickFocus = false;
                break;
            case R.id.action_color_picker:
                mBrushDrawable.getDrawable(RB_IDX).setAlpha(255);
                mCutDrawable.getDrawable(RB_IDX).setAlpha(255);
                mEraserDrawable.getDrawable(RB_IDX).setAlpha(255);
                mTextureDrawable.getDrawable(RB_IDX).setAlpha(255);

                break;
        }
    }
    
    private void changeToolBackAlpha() {
    	mBrushDrawable.getDrawable(0).setAlpha(0);
    	mEraserDrawable.getDrawable(0).setAlpha(0);
    	mTextureDrawable.getDrawable(0).setAlpha(0);
    	mCutDrawable.getDrawable(0).setAlpha(0);
    }
    
    public void clear() {
    	mBrushDrawable.setCallback(null);
    	mEraserDrawable.setCallback(null);
    	mTextureDrawable.setCallback(null);
    	mCutDrawable.setCallback(null);
    	mBrushDrawable = null;
    	mEraserDrawable = null;
    	mTextureDrawable = null;
    	mCutDrawable = null;
    }

}
